package com.woyou.ui.fragment;

import retrofit.RetrofitError;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.citaq.ideliver.R;
import com.woyou.bean.CodeResult;
import com.woyou.bean.UserInfo;
import com.woyou.bean.rpc.OrderCommentsReq;
import com.woyou.service.NetWorkCenter;
import com.woyou.ui.activity.orderdetail.OrderDetailActivity;
import com.woyou.ui.api.RefreshOrderDetailsListener;
import com.woyou.ui.component.CustomRatingBar;
import com.woyou.ui.component.CustomRatingBar.IRatingBarCallbacks;
import com.woyou.ui.view.SuperLayout;
import com.woyou.utils.Dialog;
import com.woyou.utils.LogUtil;
import com.woyou.utils.ParseUtils;

/**
 * 评价订单
 * 
 * @author shenzhou 2015年1月16日
 */
public class AssessmentOrderLayout extends SuperLayout {

	private static final String TAG = "AssessmentOrderLayout";

	private EditText edit;
	private CustomRatingBar customRatingBar;
	private TextView confrim;

	private float score = 1.0f;
	private String content = "";
	private Dialog dialog = new Dialog();
	private RefreshOrderDetailsListener mListener;

	private String sId;
	private String oId;

	public void show(String sId, String oId) {
		this.oId = oId;
		this.sId = sId;
		this.setVisibility(View.VISIBLE);
		LogUtil.d(TAG, "oId" + oId);
	}

	public void dismiss() {
		this.setVisibility(GONE);
	}

	public void setListener(RefreshOrderDetailsListener mListener) {
		this.mListener = mListener;
	}

	public AssessmentOrderLayout(Context context) {
		super(context);
		initView();
	}

	public AssessmentOrderLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	public AssessmentOrderLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}

	private void initView() {
		View view = View.inflate(mContext, R.layout.layout_assessment_order, this);
		edit = (EditText) view.findViewById(R.id.assessment_edit);
		customRatingBar = (CustomRatingBar) view.findViewById(R.id.CustomRatingBar);
		customRatingBar.setHalfStars(false);
		confrim = (TextView) view.findViewById(R.id.assessment_confirm);

		confrim.setOnClickListener(this);
		view.findViewById(R.id.assessment_cancel).setOnClickListener(this);
		view.findViewById(R.id.assessment_rl).setOnClickListener(this);

		initEvent();
	}

	private void initEvent() {
		edit.addTextChangedListener(new TextWatcher() {
			boolean flag = true;

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.toString().length() > 100) {
					if (flag) {
						content = s.toString().substring(0, 99);
						flag = false;
					}
					edit.setText(content);
					showToast("您输入的内容超过100字了");
				} else {
					content = s.toString();
				}
			}
		});
		customRatingBar.setOnScoreChanged(new IRatingBarCallbacks() {

			@Override
			public void scoreChanged(float s) {
				score = s * 2;
				if (score == 0) {
					confrim.setEnabled(false);
					confrim.setBackgroundColor(0xfff0f0f0);
				} else {
					confrim.setEnabled(true);
					confrim.setBackgroundColor(Color.WHITE);

				}
			}
		});
		customRatingBar.setScore(0);
	}

	@SuppressWarnings("rawtypes")
	public void sendAssessmentInfo() {
		if (!NetWorkCenter.isNetworkConnected(getContext())) {
			showToastOnUI("网络根本没连,赶紧检查");
			return;
		}
		executeTask(new Runnable() {

			@Override
			public void run() {
				showProgressDialog();
				try {
					OrderCommentsReq orderCommentsReq = new OrderCommentsReq();
					UserInfo userInfo = mUserModel.getUserInfo();
					if (userInfo != null) {
						orderCommentsReq.setuId(userInfo.getuId());
						orderCommentsReq.setPwd(userInfo.getPwd());
					}
					orderCommentsReq.setoId(oId);
					orderCommentsReq.setsId(sId);
					orderCommentsReq.setScore(score);
					orderCommentsReq.setComments(content);
					final CodeResult result = mOrderModel.orderComments(orderCommentsReq);
					if (result != null && result.getCode() == 1) {
						showToastOnUI("订单评论成功!");
						// 发送事件刷新订单详情
						if (mListener != null) {
							mListener.callBack(oId);
						}
						
					} else if (result != null && result.getCode() == -2) {
						showToastOnUI("很抱歉,服务器又任性了");

					} else if (result != null && result.getCode() == -3) {
						runOnUI(new Runnable() {

							@Override
							public void run() {
								dialog.LoginToast(getContext(), result.getMsg(), OrderDetailActivity.class);
							}
						});

					} else {
						showToastOnUI(result.getMsg());

					}
				} catch (RetrofitError e) {
					showToastOnUI("很抱歉,服务器又任性了");
					switch (e.getKind()) {
					case HTTP:
						return;
					case CONVERSION:
						return;
					case NETWORK:
						return;
					case UNEXPECTED:
						return;
					}
				} finally {
					dismissProgressDialog();
				}
			}
		});
	}

	public void clear() {
		customRatingBar.setScore(0);
		confrim.setEnabled(false);
		edit.setText("");
		confrim.setBackgroundResource(R.color.F0F0F0);
	}

	/**
	 * 防止View的事件穿透
	 */
	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.assessment_confirm:
			// 获取输入框的内容
			content = edit.getText().toString();
			content = ParseUtils.replaceBlank(content);
			if (content.length() > 100) {
				showToast("您输入的内容超过100字了");
			} else {
				sendAssessmentInfo();
			}
			// if ( CharCheckUtil.isEmpty(content) ){
			// Toast.makeText(getContext(), "评论的内容不能为空！",
			// Toast.LENGTH_SHORT).show();
			// } else {
			// sendAssessmentInfo();
			// }
			// sendAssessmentInfo();
			clear();
			this.setVisibility(View.GONE);
			break;

		case R.id.assessment_cancel:
			// clear();
			this.setVisibility(View.GONE);
			break;

		case R.id.assessment_rl:
			hideSoftInput(getContext(), edit);
			this.setVisibility(View.GONE);
			break;
		}
	}

}
